// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================

package tc.crase.cmds;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

import tc.common.utility.GrView;
import tc.common.utility.TcCmd;

import tc.crase.view.ViewList;
import tc.crase.view.ViewRoot;

public class CmdList extends CmdEx  implements ActionListener {
	JTextField typeField;
	JTextField attrnameField;
	JTextField containsField;
	JCheckBox graphCheck;
	
	public CmdList() {
	}
	
	public void initializeDialog() {
		super.initializeDialog();
		setLabel("Edit");
		setHint("Enter filter and click <b>Create</b> button.");
		
		JLabel     typeLabel = new JLabel("Type:");
		typeField = new JTextField();
		typeField.setPreferredSize(new Dimension(100,24));
		typeField.setMaximumSize(new Dimension(100,24));
		
		JLabel     attrnameLabel = new JLabel("Attribute Name:");
		attrnameField = new JTextField();
		attrnameField.setPreferredSize(new Dimension(100,24));
		attrnameField.setMaximumSize(new Dimension(100,24));
		
		JLabel     containsLabel = new JLabel("Contains:");
		containsField = new JTextField();
		containsField.setPreferredSize(new Dimension(SIDEBAR_WIDTH,24));
		containsField.setMaximumSize(new Dimension(SIDEBAR_WIDTH,24));
		
		graphCheck = new JCheckBox("Contains Graph",true);
		graphCheck.addActionListener(this);
		graphCheck.setAlignmentX(0.0f);
		graphCheck.setPreferredSize(new Dimension(TcCmd.SIDEBAR_WIDTH,24));
		graphCheck.setMaximumSize(new Dimension(TcCmd.SIDEBAR_WIDTH,24));
		graphCheck.setActionCommand("gridCheck");
		graphCheck.setSelected(false);
		
		JButton createButton = new JButton("Open");
		createButton.addActionListener(this);
		createButton.setActionCommand("create");
		createButton.setMaximumSize(new Dimension(SIDEBAR_WIDTH,32));
		createButton.setPreferredSize(new Dimension(SIDEBAR_WIDTH,32));
	
		dialog.add(typeLabel);
		dialog.add(typeField);
		dialog.add(containsLabel);
		dialog.add(containsField);
		dialog.add(Box.createRigidArea(new Dimension(0,6)));
		dialog.add(graphCheck);
		dialog.add(Box.createRigidArea(new Dimension(0,30)));
		dialog.add(createButton);
		
		finalizeDialog();
	}
	
	public void enterPoint(Point pt, GrView view) {
	}
	
	public void enterReset(GrView view) {
	}
	
	public void enterMove(Point pt, GrView view)  {
	}
	
	public void actionPerformed(ActionEvent e) {
		if ("create".equals(e.getActionCommand())) {
			//"^The" - matches any string that starts with "The".
			
			//String contains = containsField.getText().isEmpty() ? "" : containsField.getText();
			//String attrname = attrnameField.getText().isEmpty() ? "" : attrnameField.getText();
			ViewList list = new ViewList(typeField.getText(), attrnameField.getText(), containsField.getText(),
					                                              graphCheck.isSelected());
			
			String title = "List type=" + typeField.getText() + " contains=" + containsField.getText();
			ViewRoot.createFrame(list.view, title);
			
			typeField.setText("");
			containsField.setText("");
			attrnameField.setText("");
			graphCheck.setSelected(false);
		}
	}
}
